package com.njtoyo.taxi.admin.rest.controller.local_service.commodity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.mapper.master.entity.local_service.LifeMerchantCommodity;
import com.njtoyo.taxi.admin.rest.presenter.local_service.commodity.CommodityDetailsPresenter;
import com.njtoyo.taxi.admin.rest.presenter.local_service.commodity.CommodityPagePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.commodity.CommodityAddWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.commodity.CommodityBatchUpdateWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.commodity.CommodityPageWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.local_service.commodity.CommodityUpdateWrapper;
import com.njtoyo.taxi.admin.service.local_service.commodity.CommodityService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;

@RestController
@Api(value = "商品管理", tags = "商品管理")
@RequestMapping("local-service/commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @GetMapping("/all")
    @ApiOperation(value = "所有商品", notes = "所有商品,权限：")
    RestResult<List<LifeMerchantCommodity>> all() {
        return commodityService.all();
    }

    @GetMapping("/all/{merchantId}")
    @ApiOperation(value = "商户下所有商品", notes = "商户下所有商品,权限：")
    RestResult<List<LifeMerchantCommodity>> allByMerchantId(@PathVariable("merchantId") Long merchantId) {
        return commodityService.allByMerchantId(merchantId);
    }

    @GetMapping("")
    @ApiOperation(value = "商品列表", notes = "商品列表,权限：life_commodity_list")
    @PermissionScope(names = {"life_commodity_list"})
    RestResult<Page<CommodityPagePresenter>> getPage(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser,
                                                     @Valid CommodityPageWrapper wrapper) {
        return commodityService.getPage(adminUser, wrapper);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "商品详情", notes = "商品详情,权限:life_commodity_list")
    @PermissionScope(names = {"life_commodity_list"})
    RestResult<CommodityDetailsPresenter> details(@PathVariable("id") Long id) {
        return commodityService.details(id);
    }

    @PostMapping("")
    @ApiOperation(value = "添加商品", notes = "添加商品,权限:life_commodity_add")
    @PermissionScope(names = {"life_commodity_add"})
    RestResult add(@Valid @RequestBody CommodityAddWrapper wrapper) throws ParseException {
        return commodityService.add(wrapper);
    }

    @PatchMapping("/{id}")
    @ApiOperation(value = "修改商品", notes = "修改商品 ,权限:life_commodity_update")
    @PermissionScope(names = {"life_commodity_update"})
    RestResult update(@PathVariable("id") Long id,
                      @Valid @RequestBody CommodityUpdateWrapper wrapper) throws ParseException {
        return commodityService.update(id, wrapper);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "修改商品", notes = "修改商品 ,权限:life_commodity_delete")
    @PermissionScope(names = {"life_commodity_delete"})
    RestResult delete(@PathVariable("id") Long id) {
        return commodityService.delete(id);
    }

    @PatchMapping("/batch")
    @ApiOperation(value = "批量操作商品状态", notes = "批量操作商品状态 ,权限:life_commodity_update")
    @PermissionScope(names = {"life_commodity_update"})
    RestResult updateBatch(@Valid @RequestBody CommodityBatchUpdateWrapper wrapper) {
        return commodityService.updateBatch(wrapper);
    }

}
